"""
错题管理器
"""
import json

"""
JSON格式
[
    [题干1,A,B,C,D,TrueKey,WorryKey],
    [题干2,A,B,C,D,TrueKey,WorryKey],
    [题干3,A,B,C,D,TrueKey,WorryKey]
]
"""


class WorryManager:
    def __init__(self, UserName):
        self.username = UserName
        self.filePath = './WorryBank'
        try:
            with open(f"{self.filePath}/{self.username}.json", 'r') as f:
                self.userWorry = json.load(f)
        except:
            with open(f"{self.filePath}/{self.username}.json", 'w') as f:
                json.dump([], f)
                self.userWorry = []

    def WriteWorry(self, WorryList):
        print(f"记录错题:{WorryList}")
        self.userWorry += WorryList
        self.write()

    def write(self):
        userWorry_tmp = []
        for i in self.userWorry:
            if self.userWorry not in userWorry_tmp:
                userWorry_tmp.append(self.userWorry)
            else:
                continue
        with open(f"{self.filePath}/{self.username}.json", 'w') as f:
            json.dump(self.userWorry, f)

    def read(self):
        with open(f"{self.filePath}/{self.username}.json", 'r') as f:
            self.userWorry = json.load(f)
        return self.userWorry
